
using System;
using Gtk;
using Glade;

namespace SharpKnocking.Common.Widgets
{
   	/// <summary>
   	/// This class implements a dialog which allows specify how to 
   	/// create a list of random ports.
   	/// </summary>
	public class RandomPortGeneratorDialog
	{   
	    #region Widget Attributes
	
    	[WidgetAttribute]
    	Dialog randomPortGeneratorDialog;
    	
    	[WidgetAttribute]
    	SpinButton portNumberSpinButton;
    	
    	[WidgetAttribute]
    	Button okButton;

		[WidgetAttribute]
    	Button cancelButton;
    	
    	[WidgetAttribute]
    	CheckButton overwriteCheck;
    	
    	#endregion
				
		/// <summary>
		/// This constructor creates a window to edit a port, initially set as 0.
		/// </summary>
		/// <param name = "parent">
		/// The dialog's parent <c>Dialog</c>.
		/// </param>
		/// <param name = "existingPorts">
		/// Tells the instance if there is already a port list.
		/// </param>
		public RandomPortGeneratorDialog(Window parent, bool existingPorts)
        {
            Glade.XML gxml =
            	new Glade.XML (null, "gui.glade", "randomPortGeneratorDialog", null);
            	
			gxml.Autoconnect (this);
			
			randomPortGeneratorDialog.TransientFor = parent;
			randomPortGeneratorDialog.Modal =  true;			
			
			randomPortGeneratorDialog.Icon=Gdk.Pixbuf.LoadFromResource("sharpknocking22.png");
						
			// We force the spinbutton to grab focus, so the content
			// will be overwritten as the user types.
			portNumberSpinButton.GrabFocus();	
			
			overwriteCheck.Sensitive = existingPorts;
        }        
        
        #region Properties
        
        /// <summary>
        /// This property allows the value established the number of ports to 
        /// be created.
        /// </summary>
        public int PortNumber
        {
            get
            {
                return portNumberSpinButton.ValueAsInt;
            }
        }
        
        /// <summary>
        /// This property allows the user object to know if the exiting ports
        /// should be removed.
        /// </summary>
        public bool Overwrite
        {
        	get
        	{
        		return overwriteCheck.Active;
        	}
        }
        
        #endregion Properties
        
        #region Public methods
        
        /// <summary>
        /// This method allows the caller to wait until a response has been 
        /// generated by the dialog.
        /// </summary>
        public ResponseType Run()
        {
            return (ResponseType)(randomPortGeneratorDialog.Run());
        
        } 
        
        /// <summary>
        /// This method frees the resourses given to the dialog.
        /// </summary>
        public void Destroy()
        {
        	randomPortGeneratorDialog.Destroy();
        }
        
        #endregion Public methods
        
        #region Private methods
        
        private void OnOkButtonClicked(object sender, EventArgs a)
        {        
			randomPortGeneratorDialog.Respond(ResponseType.Ok);
        }
        
        #endregion Private methods
    }
}